package com.tjsinfo.tjsoa.system.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tjsinfo.tjsoa.common.SessionUser;
import com.tjsinfo.tjsoa.system.bo.ISystemAllGroupBO;
import com.tjsinfo.tjsoa.system.bo.ISystemPrivateGroupBO;
import com.tjsinfo.tjsoa.system.vo.TSystemAllgroup;
import com.tjsinfo.tjsoa.system.vo.TSystemPrivategroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.TSystemUserallgroup;
import com.tjsinfo.tjsoa.system.vo.TSystemUserprivategroup;

public class UpdatePrivateGroupAction extends Action {
	private ISystemPrivateGroupBO systemPrivateGroupBO;
	
	

	public ISystemPrivateGroupBO getSystemPrivateGroupBO() {
		return systemPrivateGroupBO;
	}



	public void setSystemPrivateGroupBO(ISystemPrivateGroupBO systemPrivateGroupBO) {
		this.systemPrivateGroupBO = systemPrivateGroupBO;
	}



	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		List<TSystemUserprivategroup> systemUserallgroupList = new ArrayList<TSystemUserprivategroup>();
		TSystemPrivategroup systemAllgroup = new TSystemPrivategroup();
		systemAllgroup.setGroupName(request.getParameter("groupName"));
		systemAllgroup.setOrderNo(Integer.parseInt(request.getParameter("orderNo")));
		systemAllgroup.setState(0);
		systemAllgroup.setSessionId(SessionUser.getUserId(request));
		systemAllgroup.setId(Integer.parseInt(request.getParameter("id")));
		String userinfoListId = request.getParameter("renyuanid");
		if (userinfoListId != null && !"".equals(userinfoListId)) {
			String[] userinfoArray = userinfoListId.split(",");
			for (String userid : userinfoArray) {
				if(userid != null && !"".equals(userid)){
					TSystemUser systemUser = new TSystemUser();
					systemUser.setUserId(Integer.parseInt(userid));
					TSystemUserprivategroup systemUserallgroup = new TSystemUserprivategroup();
					systemUserallgroup.setSystemUser(systemUser);
					systemUserallgroupList.add(systemUserallgroup);
				}
			}
		} else {
			;
		}
		systemPrivateGroupBO.deleteTSystemUserPrivategroupById(systemAllgroup.getId());
		for (TSystemUserprivategroup systemUserallgroup : systemUserallgroupList) {
			systemUserallgroup.setSystemPrivategroup(systemAllgroup);
			systemPrivateGroupBO.addTSystemUserPrivategroupById(systemUserallgroup);
		}
		systemPrivateGroupBO.updateSystemPrivateGroup(systemAllgroup);
		request.setAttribute("linkurl", "system/privateGroup/privatelgrouplist.jsp");
		request.setAttribute("message", "system.save.message");
		return mapping.findForward("success");
	}
}
